package com.zzyl.nursing.service.impl;

import java.util.List;
import java.util.Set;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.vo.NursingProjectVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.service.INursingProjectService;

/**
 * 护理项目Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-05
 */
@Service
@Slf4j
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    private static final String CACHE_KEY_NURSING_PROJECT = "nursing:project:";

    /**
     * id查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return nursingProjectMapper.selectNursingProjectById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        //检查护理项目是否已经存在
        LambdaQueryWrapper<NursingProject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingProject::getName, nursingProject.getName());
        if (nursingProjectMapper.selectOne(queryWrapper) != null) {
            throw new BaseException("护理项目已存在");
        }

        nursingProject.setCreateTime(DateUtils.getNowDate());
        int flag = nursingProjectMapper.insertNursingProject(nursingProject);
        if (flag > 0) {
            // 删除缓存
            deleteCache();
        }
        return flag;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        nursingProject.setUpdateTime(DateUtils.getNowDate());
        //更新数据库
        int flag = nursingProjectMapper.updateNursingProject(nursingProject);
        if (flag > 0) {
            // 删除缓存
            deleteCache();
        }
        return flag;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        int flag = nursingProjectMapper.deleteNursingProjectByIds(ids);
        if (flag > 0) {
            deleteCache(); // 删除缓存
        }
        return flag;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        int flag = nursingProjectMapper.deleteNursingProjectById(id);
        if (flag > 0) {
            deleteCache(); // 删除缓存
        }
        return flag;
    }

    /**
     * 删除缓存
     */
    private void deleteCache() {
        // 使用keys方法获取所有匹配的键，然后批量删除
        Set<Object> keys = redisTemplate.keys(CACHE_KEY_NURSING_PROJECT + "*");
        if (keys != null && !keys.isEmpty()) {
            redisTemplate.delete(keys);
        }
    }

    /**
     * 查询所有护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> selectAll() {
        //查询缓存
        List<NursingProjectVo> list = (List<NursingProjectVo>) redisTemplate.opsForValue().get(CACHE_KEY_NURSING_PROJECT + "all");
        if (list != null) {
            log.info("查询护理项目缓存");
            return list;
        }
        //数据库查询所有护理项目
        list = nursingProjectMapper.selectAll();
        log.info("没有查询到缓存,查询护理项目数据库");
        //写入缓存
        redisTemplate.opsForValue().set(
                CACHE_KEY_NURSING_PROJECT + "all", list,
                60 * 60 * 24, java.util.concurrent.TimeUnit.SECONDS
        );
        return list;
    }
}
